/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shapematch;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import net.jasonbarnwell.java.BackgroundPanel;

/**
 *
 * @author Jason Barnwell
 * 13/0719/2191
 */
public class ShapeMatchUI extends JFrame{
        
    private GameEngine engine;
    
    private JLabel statusLabel;
    
    private JPanel menuPanel;
    private BackgroundPanel gamePanel;
    private JPanel statusBarPanel;
    
    private JLabel winStats, lossStats, timeStats, moveStats;
    
    private JPanel statsPanel, aboutPanel;
    
    public ShapeMatchUI() {
        super("Shape Match");
        
        engine = new GameEngine();
        
        statusLabel = new JLabel("Click New Game to Begin");
        
        menuPanel = new JPanel();
        gamePanel = new BackgroundPanel(new ImageIcon("images/gameover.png").getImage(), BackgroundPanel.ACTUAL);
        statusBarPanel = new JPanel();
        
        winStats = new JLabel();
        lossStats = new JLabel();
        moveStats =  new JLabel();
        timeStats = new JLabel();
        
        statsPanel = new JPanel();
        aboutPanel= new JPanel();
        
        setPanelLayouts();
        
        setupMenuPanel();
        setupStatusPanel();
        setupStatsPanel();
        
        addPanelsToUI();
        
        startNewGame();
    }
    
    private void addTileListToUI(LinkedList<ShapeTile> shapeTiles){
        clearGamePanel();
        for(final ShapeTile tile: shapeTiles) {
            gamePanel.add(tile);
        }
    }
    
    private void clearGamePanel(){
        gamePanel.removeAll();
        gamePanel.revalidate();
    }
    
    private void startNewGame(){
       NewGameDialog newGameDialog = new NewGameDialog();
        int difficulty = newGameDialog.requestDifficulty();
        if (difficulty>0){
            engine.newGame(difficulty);
            addTileListToUI(engine.getShapeTilesList());
            
            /* maybe not the best way it'll do for now */
            if (difficulty==GameEngine.HARD){
                gamePanel.setLayout(new GridLayout(0,6));
            }
            else {
                gamePanel.setLayout(new GridLayout(0,4));
            }
            statusLabel.setText("New Game Started");
        }
        //System.out.println(newGameDialog.requestDifficulty());
    }
    
    private void showStatsDialog(){
        winStats.setText("Wins: "+engine.getWins());
        lossStats.setText("Losses: "+engine.getLosses());
        moveStats.setText("Fewest Moves: "+engine.getFewestMoves());
        timeStats.setText("Fastest Time: "+engine.getFastestTime()+" seconds");
        JOptionPane.showMessageDialog(null, statsPanel, "Statistics", JOptionPane.PLAIN_MESSAGE);
    }
    
    private void showAboutDialog(){
        aboutPanel.removeAll();
        aboutPanel.add(new JLabel("ShapeMatch v2.1 [ 03 November 2013 ] "));
        aboutPanel.add(new JLabel("Developed by Jason Barnwell"));
        aboutPanel.add(new JLabel("www.jasonbarnwell.net"));
        
        JOptionPane.showMessageDialog(null, aboutPanel, "About", JOptionPane.PLAIN_MESSAGE);
    }
    
    private void setPanelLayouts(){
        menuPanel.setLayout(new BorderLayout());
        
        statusBarPanel.setLayout(new BorderLayout());
        
        gamePanel.setLayout(new GridLayout(0,4));
        
        statsPanel.setLayout(new BoxLayout(statsPanel, BoxLayout.Y_AXIS));
        aboutPanel.setLayout(new BoxLayout(aboutPanel, BoxLayout.Y_AXIS));
    }
    
    private void setupMenuPanel(){
        JPanel  menuPanel_left = new JPanel(), menuPanel_right = new JPanel();
        menuPanel_left.setLayout(new FlowLayout(FlowLayout.LEFT));
        menuPanel_right.setLayout(new FlowLayout(FlowLayout.RIGHT));
        
        JButton newGameBtn = new JButton("New Game"/*, UIManager.getIcon("Tree.leafIcon")*/);
        JButton restartGameBtn = new JButton("Restart Game");
        JButton viewStatsBtn = new JButton("Statistics");
        JButton aboutGameBtn = new JButton("About");
        JButton quitGameBtn = new JButton("Quit"/*, UIManager.getIcon("OptionPane.errorIcon")*/);

        /*Event Handlers*/
        newGameBtn.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent event) {
                startNewGame();
            }
        });
        
        restartGameBtn.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent event) {
                engine.restartGame();
                statusLabel.setText("Game Restarted");
            }
        });
        
        viewStatsBtn.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent event) {
                showStatsDialog();
            }
        });
        
        aboutGameBtn.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent event) {
                showAboutDialog();
            }
        });
        
        quitGameBtn.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        
        menuPanel_left.add(newGameBtn);
        menuPanel_left.add(restartGameBtn);
        menuPanel_left.add(viewStatsBtn);
        menuPanel_right.add(aboutGameBtn);
        menuPanel_right.add(quitGameBtn);
        
        menuPanel.add(menuPanel_left, BorderLayout.WEST);
        menuPanel.add(menuPanel_right, BorderLayout.EAST);
        
    }
    
    private void setupStatusPanel(){
        JPanel  statusBarPanel_left = new JPanel(), statusBarPanel_right = new JPanel();
        
        statusBarPanel_left.setLayout(new FlowLayout(FlowLayout.LEFT));
        statusBarPanel_right.setLayout(new FlowLayout(FlowLayout.RIGHT));
        
        statusBarPanel_left.add(statusLabel);
        statusBarPanel_right.add(new JLabel("Time Remaining: "));
        statusBarPanel_right.add(engine.getUpdatedJlabel());
        statusBarPanel.add(statusBarPanel_left, BorderLayout.WEST);
        statusBarPanel.add(statusBarPanel_right, BorderLayout.EAST);
    }
    
    private void setupStatsPanel(){
        statsPanel.add(winStats);
        statsPanel.add(lossStats);
        statsPanel.add(moveStats);
        statsPanel.add(timeStats);
    }
    
    private void addPanelsToUI(){
        add(menuPanel, BorderLayout.NORTH);
        add(gamePanel, BorderLayout.CENTER);
        add(statusBarPanel, BorderLayout.SOUTH);
    }
    
}
